﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Controller;
using Interfaces;
using Model;
using System.Collections;

// Print
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Net;

namespace GUI
{
    public partial class TurneringsSkema : Form
    {
        #region Attributes
        // Print
        private System.IO.Stream streamToPrint;
        string streamType;

        TurneringsSystem control;
        ITurnering aktuelTurnering = null;
        IRunde aktuelRunde;
        private const string FILNAVN = "DataLokal.bin";
        int antalRunder = 0;
        int rundeNummer = 1;   
        int deltagerID, spillerTæller, antalSpillere, gammelAntalRunder;
        bool nyOpstart;

        double resultat;
        double resultatIalt;
        #endregion

        #region Contructor
        public TurneringsSkema(TurneringsSystem control, int turneringsID)
        {
            InitializeComponent();
            this.control = control;
            OpstartTurnering(turneringsID);
        }
        #endregion

        #region Rediger tilstand

        private void RedigerResultat()
        {
            btnIndskrivResultater.Enabled = false;
            btnBeregnScore.Enabled = false;
            btnPrintTurneringskort.Enabled = false;
            btnRedigerTurnering.Enabled = false; 
        }

        private void RedigerResultatSlut()
        {
            btnIndskrivResultater.Enabled = true;
            btnBeregnScore.Enabled = true;
            btnPrintTurneringskort.Enabled = true;
            btnRedigerTurnering.Enabled = true;
        }
        #endregion

        #region Print funktion

        // PrintScreen af boks ?????
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt
        (
            IntPtr hdcDest, // handle to destination DC
            int nXDest, // x-coord of destination upper-left corner
            int nYDest, // y-coord of destination upper-left corner
            int nWidth, // width of destination rectangle
            int nHeight, // height of destination rectangle
            IntPtr hdcSrc, // handle to source DC
            int nXSrc, // x-coordinate of source upper-left corner
            int nYSrc, // y-coordinate of source upper-left corner
            System.Int32 dwRop // raster operation code
        );

        // ??????????
        private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
            int x = e.MarginBounds.X;
            int y = e.MarginBounds.Y;
            int width = image.Width;
            int height = image.Height;
            if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
            {
                width = e.MarginBounds.Width;
                height = image.Height * e.MarginBounds.Width / image.Width;
            }
            else
            {
                height = e.MarginBounds.Height;
                width = image.Width * e.MarginBounds.Height / image.Height;
            }
            System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
            e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
        }

        // ??????????????? 
        public void StartPrint(Stream streamToPrint, string streamType)
        {
            this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
            this.streamToPrint = streamToPrint;
            this.streamType = streamType;
            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
            PrintDialog1.AllowSomePages = true;
            PrintDialog1.ShowHelp = true;
            PrintDialog1.Document = printDoc;

            DialogResult result = PrintDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                printDoc.Print();
                //docToPrint.Print();
            }

        }

        // Starter udskrivningen, åbner print Dialogbox, hvor printer kan vælges
        private void StartUdskrivningDialogBox()
        {
            Graphics g1 = this.CreateGraphics();
            Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
            Graphics g2 = Graphics.FromImage(MyImage);
            IntPtr dc1 = g1.GetHdc();
            IntPtr dc2 = g2.GetHdc();
            BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369377);
            g1.ReleaseHdc(dc1);
            g2.ReleaseHdc(dc2);
            FileStream fileStream = new FileStream("turneringsKortPrint.jpg", FileMode.Open, FileAccess.Read);
            StartPrint(fileStream, "Image");
            fileStream.Close();
        }
        

        // starter udskrivnings processen 
        private void btnPrintTurneringskort_Click(object sender, EventArgs e)
        {
            int UdskrevetSpillerTæller = 0;

            IEnumerator deltageriterator = control.GetDeltagerIterator();
            deltageriterator.Reset();
            int A4 = 0;
            while (deltageriterator.MoveNext())
            {                
                IDeltager aktuelDeltager = (IDeltager)deltageriterator.Current;
                deltagerID = aktuelDeltager.DeltagerID;

                TurneringsKort turneringskortGUI = new TurneringsKort(control, deltagerID, aktuelTurnering.TurneringsID);

                
                turneringskortGUI.ShowDialog();
                A4++;
                // Kontroler om der er 4 Turneringskort på en side.
                // Hvis der er så udskrives de
                if (A4 == 4)
                {
                    StartUdskrivningDialogBox();
                    A4 = 0;
                    UdskrevetSpillerTæller = UdskrevetSpillerTæller + 4;
                    System.IO.File.Delete("turneringsKortPrint.jpg");
                }                                
            }
            // Kontroler om alle spiller er udskrevet, hvis ikke udskriver resten
            // Dette er kun tilfælde når antal spiller ikke kan deles med 4
            if ((antalSpillere - UdskrevetSpillerTæller) != 0 )
            {
                StartUdskrivningDialogBox();
                System.IO.File.Delete("turneringsKortPrint.jpg");
            }
        }


        
        #endregion

        
        private void OpstartTurnering(int turneringsID = 0)
        {
            // Kørers kun hvis man har et ID
            if (turneringsID != 0)
            {
                try
                {
                    this.aktuelTurnering = control.FindTurnering(turneringsID);
                    this.antalRunder = aktuelTurnering.AntalRunder;
                    SkjulAllePanels();
                    IndlæsSpillerNavne();
                    SkjulTekstBoxe();
                    JusterAntalRunder();
                    FindModstandere();
                    nyOpstart = true;
                }
                catch
                {
                    MessageBox.Show("Kunne ikke indlæse turneringen");
                }
            }

            // Opsæt Turnering hvis den blev fundet
            if (aktuelTurnering != null && nyOpstart == false)
            {
                SkjulAllePanels();
                IndlæsSpillerNavne();
                SkjulTekstBoxe();
                antalRunder = aktuelTurnering.AntalRunder;
                if (gammelAntalRunder != antalRunder)
                {
                    JusterAntalRunder();
                }
                FindModstandere();
            }
            nyOpstart = false;
        }

        private void IndlæsSpillerNavne()
        {
            // Nulstiller antal spillere
            antalSpillere = 0;

            IDeltager deltagerdata;
            IEnumerator deltageriterator = control.GetDeltagerIterator();
            List<IDeltager> deltagerListe = new List<IDeltager>();
            deltageriterator.Reset();

            // Gennemløber det hele og ligger det ind på Turneringen
            while (deltageriterator.MoveNext())
            {
                antalSpillere++;
                deltagerdata = (IDeltager)deltageriterator.Current;
                if (deltagerdata.Rating == 0)
                {
                    string sum = deltagerdata.Navn + " " + deltagerdata.Efternavn;
                    this.Controls.Find("lblSpiller" + deltagerdata.Nr, true)[0].Text = sum.ToString();
                }
                else
                {
                    string sum = deltagerdata.Navn + " " + deltagerdata.Efternavn + " (" + deltagerdata.Rating + ")";
                    this.Controls.Find("lblSpiller" + deltagerdata.Nr, true)[0].Text = sum.ToString();
                }
                
                

                IDeltager aktuelDeltager = (IDeltager)deltageriterator.Current;
                control.FindDeltager(aktuelDeltager.DeltagerID);

                IEnumerator rundeiterator = aktuelDeltager.GetRundeIterator();
                rundeiterator.Reset();
                while (rundeiterator.MoveNext())
                {
                    aktuelRunde = (IRunde)rundeiterator.Current;
                    this.Controls.Find("lblPlaceringS" + deltagerdata.Nr, true)[0].Text = aktuelDeltager.Nr.ToString();
                }

                deltagerID = deltagerdata.DeltagerID;
            }

            JusterSkemaStørrelse();
        }

        #region Opsætning af Skema

        private void JusterAntalRunder()
        {
            gammelAntalRunder = antalRunder;
            // Fjerner rundter der ikke spilles
            int a = 25;
            int b = 9;
            int c = 0;

            while (b != antalRunder)
            {
                c = a;
                while (a <= 151)
                {
                    this.Controls.Find("panel" + a, true)[0].Visible = false;
                    a = a + 14;
                }
                a = c - 1;
                b--;
            }
        }

        private void JusterSkemaStørrelse()
        {
            // Viser antal spiller og informationer (i = antal deltager)
            int panelNr = 1;
            while (panelNr <= (antalSpillere * 14))
            {
                this.Controls.Find("panel" + panelNr, true)[0].Visible = true;
                panelNr++;
            }
        }

        private void SkjulAllePanels()
        {
            // Skjuler alle Panels
            int panelNr = 1;
            while (panelNr != 141)
            {
                this.Controls.Find("panel" + panelNr, true)[0].Visible = false;
                panelNr++;
            }
        }

        private void SkjulTekstBoxe()
        {
            // Skjuler bokse der ikke skal bruges i nu
            int i = 1;
            int r = 1;
            
            // i er spiller antal
            while (i <= 10)
            {
                // r er runde antal
                while (r <= 9)
                {
                    this.Controls.Find("txtS"+ i + "R" + r, true)[0].Visible = false;
                    r++;
                }
                r = 1;
                i++;
            }
        }

        // Viser alle tekst bokse for deltager i turneringen
        private void VisAlleTekstBoxe()
        {
            int i = 1;
            int r = 1;

            // Spiller
            while (i <= 10)
            {
                // Runde
                while (r <= 9)
                {
                    this.Controls.Find("txtS" + i + "R" + r, true)[0].Visible = true;
                    r++;
                }
                r = 1;
                i++;
            }
        }

        private void IndsætResultater()
        {
            int s = 1;
            // Viser bokse der skal bruges

            s = 1;
            while (s < 11)
            {
                this.Controls.Find("txtS" + s + "R" + rundeNummer, true)[0].Visible = true;
                s++;
            }
            s = 1;                    
        }
        #endregion

        private void btnLuk_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void btnRediger_Click(object sender, EventArgs e)
        {
            nyTurnering nyTurneringGUI = new nyTurnering(control, aktuelTurnering);
            nyTurneringGUI.ShowDialog();
            this.Close();
            TurneringsSkema turneringsSkemaGUI = new TurneringsSkema(control, aktuelTurnering.TurneringsID);
            turneringsSkemaGUI.Show();
        }

        private void btnIndskrivResultater_Click(object sender, EventArgs e)
        {
            IndsætResultater();
        }

        private void btnBeregnScore_Click(object sender, EventArgs e)
        {
            Resultater();

            while (NyPlacering <= antalSpillere)
            {
                BeregnSpillerPlacering();
                IndsætPlaceringPåAktuelRunde();
                ResetData();
            }
            NyPlacering = 1;
                         
            rundeNummer++;
            OpretRundte();
            SkjulTekstBoxe();
            FindModstandere();
        }
        

        private void ResetData()
        {
            SpillerDerSamlignesIalt = -1;
            SpillerDerSamlignesPlacering = 100;
            SpillerDerSamlignesNr = 0;
        }


        int gammelplacering;
        int SpillerDerSamlignesGammelplacering;
        int NyPlacering = 1;

        double SpillerDerSamlignesIalt = -1;
        int SpillerDerSamlignesPlacering = 100;
        int SpillerDerSamlignesNr;

        // Udregner spiller placering, metoden er opdelt den tager forbehold for den spicelle siteration der er i første runde.
        private void BeregnSpillerPlacering()
        {
            IEnumerator deltageriterator = control.GetDeltagerIterator();
            deltageriterator.Reset();

            while (deltageriterator.MoveNext())
            {
                IDeltager aktuelDeltager = (IDeltager)deltageriterator.Current;
                control.FindDeltager(aktuelDeltager.DeltagerID);

                IEnumerator rundeiterator = aktuelDeltager.GetRundeIterator();
                rundeiterator.Reset();
                while (rundeiterator.MoveNext())
                {
                    aktuelRunde = (IRunde)rundeiterator.Current;

                    if (rundeNummer == 1)
                    {
                        // Hvis det er den rigtige rundte
                        if (aktuelRunde.RundeNr == rundeNummer)
                        {
                            // Tjekker om personen har højest resultat, sorter mindre end fra.
                            if (aktuelRunde.Ialt >= SpillerDerSamlignesIalt)
                            {
                                if (aktuelRunde.Placering == 0)
                                {
                                    // Hvis personen har støre score
                                    if (aktuelRunde.Ialt > SpillerDerSamlignesIalt)
                                    {
                                        SpillerDerSamlignesIalt = aktuelRunde.Ialt;
                                        SpillerDerSamlignesNr = aktuelDeltager.Nr;
                                        SpillerDerSamlignesPlacering = aktuelDeltager.Nr;
                                    }
                                    // Hvis personen har samme score, samlinges placering også
                                    else if (aktuelDeltager.Nr < SpillerDerSamlignesPlacering)
                                    {
                                        SpillerDerSamlignesIalt = aktuelRunde.Ialt;
                                        SpillerDerSamlignesNr = aktuelDeltager.Nr;
                                        SpillerDerSamlignesPlacering = aktuelDeltager.Nr;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // Placering fra runde før
                        if (aktuelRunde.RundeNr == rundeNummer - 1)
                        {
                            gammelplacering = aktuelRunde.Placering;
                        }
                        // Fra sorter spiller der allerede har fåer placering
                        if (aktuelRunde.Placering > NyPlacering)
                        {
                            if (aktuelRunde.RundeNr == rundeNummer)
                            {
                                // Tjekker om personen har højest resultat, Personer med mindre resultat sorteres fra.
                                if (aktuelRunde.Ialt >= SpillerDerSamlignesIalt)
                                {
                                    // Hvis personen har støre score
                                    if (aktuelRunde.Ialt > SpillerDerSamlignesIalt)
                                    {
                                        // Samlignes der med den spillers oplysninger fremover
                                        SpillerDerSamlignesIalt = aktuelRunde.Ialt;
                                        SpillerDerSamlignesNr = aktuelDeltager.Nr;
                                        SpillerDerSamlignesPlacering = aktuelRunde.Placering;
                                        SpillerDerSamlignesGammelplacering = gammelplacering;
                                    }
                                    // Hvis personen har samme score, samlinges placering også
                                    else if (gammelplacering < SpillerDerSamlignesGammelplacering)
                                    {
                                        SpillerDerSamlignesIalt = aktuelRunde.Ialt;
                                        SpillerDerSamlignesNr = aktuelDeltager.Nr;
                                        SpillerDerSamlignesPlacering = aktuelRunde.Placering;
                                        SpillerDerSamlignesGammelplacering = gammelplacering;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
                
        // Indlæser nye spiller placering på runde. placering på turneringsskema.
        private void IndsætPlaceringPåAktuelRunde()
        {
            IEnumerator deltageriterator = control.GetDeltagerIterator();
            deltageriterator.Reset();

            while (deltageriterator.MoveNext())
            {
                IDeltager aktuelDeltager = (IDeltager)deltageriterator.Current;
                control.FindDeltager(aktuelDeltager.DeltagerID);

                if (aktuelDeltager.Nr == SpillerDerSamlignesNr)
                {
                    IEnumerator rundeiterator = aktuelDeltager.GetRundeIterator();
                    rundeiterator.Reset();
                    while (rundeiterator.MoveNext())
                    {
                        aktuelRunde = (IRunde)rundeiterator.Current;

                        // Hvis det er den rigtige rundte
                        if (aktuelRunde.RundeNr == rundeNummer)
                        {
                            control.RedigerRunde(rundeNummer, aktuelRunde.Modstander, aktuelRunde.Farve, aktuelRunde.Resultat, aktuelRunde.Ialt, NyPlacering);
                            control.GemData();
                            (this.Controls.Find("lblPlaceringS" + aktuelDeltager.Nr, true)[0].Text) = Convert.ToString(NyPlacering);
                            NyPlacering++;
                        }                        
                    }
                }
            }
        }

        private void OpdaterResultater()
        {
            IEnumerator deltageriterator = control.GetDeltagerIterator();
            deltageriterator.Reset();
            
            while (deltageriterator.MoveNext())
            {
                IDeltager aktuelDeltager = (IDeltager)deltageriterator.Current;
                control.FindDeltager(aktuelDeltager.DeltagerID);
                
                IEnumerator rundeiterator = aktuelDeltager.GetRundeIterator();
                rundeiterator.Reset();
                                
                // Clear Total score for spiller
                (this.Controls.Find("lblS" + aktuelDeltager.Nr + "Total", true)[0].Text) = "0";
                
                while (rundeiterator.MoveNext())
                {
                    aktuelRunde = (IRunde)rundeiterator.Current;
                    if (rundeNummer != aktuelRunde.RundeNr)
                    {
                        if ((this.Controls.Find("lblS" + aktuelDeltager.Nr + "R" + aktuelRunde.RundeNr, true)[0].Text) == "½")
                        {
                            resultat = 0.5;
                        }
                        else
                        {
                            resultat = Convert.ToDouble(this.Controls.Find("lblS" + aktuelDeltager.Nr + "R" + aktuelRunde.RundeNr, true)[0].Text);
                        }

                        // Optæller nuværenderesultat
                        double nuværendeResultat;
                        nuværendeResultat = Convert.ToDouble(this.Controls.Find("lblS" + aktuelDeltager.Nr + "Total", true)[0].Text);

                        resultatIalt = resultat + nuværendeResultat;
                        (this.Controls.Find("lblS" + aktuelDeltager.Nr + "Total", true)[0].Text) = Convert.ToString(resultatIalt);
                        
                        control.RedigerRunde(aktuelRunde.RundeNr, aktuelRunde.Modstander, aktuelRunde.Farve, resultat, resultatIalt, 0);
                        control.GemData();
                    }  
                }
            }
        }       
        
        #region Indlæser resultater af en rundte, og opdater total score i systemet

        // indlæser resultater af en rundte i systemet
        private void Resultater()
        {   
            IEnumerator deltageriterator = control.GetDeltagerIterator();
            deltageriterator.Reset();            
            while (deltageriterator.MoveNext())
            {   
                IDeltager aktuelDeltager = (IDeltager)deltageriterator.Current;

                // Sætter rigtig deltager
                control.FindDeltager(aktuelDeltager.DeltagerID);                    

                    IEnumerator rundeiterator = aktuelDeltager.GetRundeIterator();
                    rundeiterator.Reset();

                    // Gemmemløber rundter på en spiller.
                    while (rundeiterator.MoveNext())
                    {
                            
                        aktuelRunde = (IRunde)rundeiterator.Current;
                        if (rundeNummer == aktuelRunde.RundeNr)
                        {
                            if ((this.Controls.Find("lblS" + aktuelDeltager.Nr + "R" + rundeNummer, true)[0].Text) == "½")
                            {   
                                resultat = 0.5;                            
                            }                                  
                            else
                            {
                                resultat = Convert.ToDouble(this.Controls.Find("lblS" + aktuelDeltager.Nr + "R" + rundeNummer, true)[0].Text);
                            }

                            // Optæller nuværenderesultat
                            double nuværendeResultat;
                            nuværendeResultat = Convert.ToDouble(this.Controls.Find("lblS" + aktuelDeltager.Nr + "Total", true)[0].Text);

                            resultatIalt = resultat + nuværendeResultat;
                            (this.Controls.Find("lblS" + aktuelDeltager.Nr + "Total", true)[0].Text) = Convert.ToString(resultatIalt);

                            control.RedigerRunde(rundeNummer, aktuelRunde.Modstander, aktuelRunde.Farve, resultat, resultatIalt, aktuelRunde.Placering);
                            control.GemData();
                        }
                    }
                    
                    
                spillerTæller++;
            }
        }
        #endregion

        #region Opret en rundte
        private void OpretRundte()
        {
            IEnumerator deltageriterator = aktuelTurnering.GetDeltagerIterator();
            deltageriterator.Reset();

            // Opretter en rundte for hver deltager
            while (deltageriterator.MoveNext())
            {
                IDeltager aktuelDeltager = (IDeltager)deltageriterator.Current;
                control.FindDeltager(aktuelDeltager.DeltagerID);
                control.TilføjRunde(rundeNummer, 0, 1, 0, 0, 100);
                control.GemData();
            }
        }

        #endregion

        private void btnRedigerResultat_Click(object sender, EventArgs e)
        {
            if (btnRedigerResultat.Text == "Rediger Resultater")
            {
                VisAlleTekstBoxe();
                btnRedigerResultat.Text = "Opdater Resultater";
                RedigerResultat();
                control.GemData();
            }
            else
            {
                OpdaterResultater();
                btnRedigerResultat.Text = "Rediger Resultater";
                SkjulTekstBoxe();
                RedigerResultatSlut();
                FindModstandere();
            }
        }

        private void FindModstandere()
        {
            IEnumerator deltageriterator = control.GetDeltagerIterator();
            List<IDeltager> deltagerListe = new List<IDeltager>();

            // Finder Spiller med placering nr 1, derefter nr 2, derefter nr 3 osv
            for (int i = 1; i <= antalSpillere; i++)
            {
                deltageriterator.Reset();
                while (deltageriterator.MoveNext())
                {
                    IDeltager aktuelDeltager = (IDeltager)deltageriterator.Current;
                    control.FindDeltager(aktuelDeltager.DeltagerID);

                    IEnumerator rundeiterator = aktuelDeltager.GetRundeIterator();
                    rundeiterator.Reset();
                    while (rundeiterator.MoveNext())
                    {
                        aktuelRunde = (IRunde)rundeiterator.Current;
                        // Finde den rigtige runde og tjekker om placringenen passer med den placering vi er ude efter
                        if (aktuelRunde.RundeNr == (rundeNummer - 1) && aktuelRunde.Placering == i)
                        {
                            // Tilføjer deltageren til vores liste
                            deltagerListe.Add(aktuelDeltager);
                        }
                        else if ((rundeNummer - 1) == 0 && aktuelRunde.Placering == i)
                        {
                            // Tilføjer deltageren til vores liste
                            deltagerListe.Add(aktuelDeltager);
                        }
                    }
                }
            }

            SammenlignModstandere(deltagerListe);
        }

        private void SammenlignModstandere(List<IDeltager> deltagerListe)
        {
            int tæller = 0;
            bool ugyldig, hop;
            IRunde nuværendeRunde;

            // Går igennem alle deltagerne i listen (Den er sorteret efter placering)
            foreach (IDeltager nuværendeDeltager in deltagerListe)
            {
                hop = false;
                // Kører en løkke samme antal gange som der er deltagere med lavere placering
                for (int i = tæller; i < antalSpillere; i++)
                {
                    ugyldig = false;

                    // Tjekker at det ikke er sidste deltager i listen
                    IDeltager næsteDeltager = null;
                    try
                    {
                        næsteDeltager = deltagerListe[(i + 1)];
                    }
                    catch
                    {
                        // Hvis det er den sidste i listen
                    }

                    // Hvis ikke det er den sidste deltager i listen
                    if (næsteDeltager != null)
                    {
                        control.FindDeltager(nuværendeDeltager.DeltagerID);
                        IEnumerator rundeiterator = nuværendeDeltager.GetRundeIterator();

                        // Går igennem alle deltagerens runder
                        while (rundeiterator.MoveNext())
                        {
                            nuværendeRunde = (IRunde)rundeiterator.Current;

                            // Hvis der er en runde (Dog ikke den nye) hvor nuværende Deltager har haft næsteDeltager som modstander
                            if (nuværendeRunde.Modstander == næsteDeltager.Nr && nuværendeRunde.RundeNr != rundeNummer)
                            {
                                ugyldig = true;
                            }
                            // Hvis den nye runde allerede har fået sat en modstander
                            else if (nuværendeRunde.RundeNr == rundeNummer && nuværendeRunde.Modstander != 0)
                            {
                                hop = true;
                            }

                            // Sikrer at Deltageren ikke har været imod den modstander og det er den nye runde
                            if (ugyldig != true && nuværendeRunde.RundeNr == rundeNummer)
                            {
                                // Kør kun hvis runden ikke allerede er sat
                                if (hop == false)
                                {
                                    if (rundeNummer == 1)
                                    {
                                        control.RedigerRunde(rundeNummer, næsteDeltager.Nr, 0, 0, 0, 0);
                                        control.FindDeltager(næsteDeltager.DeltagerID);
                                        control.RedigerRunde(rundeNummer, nuværendeDeltager.Nr, 1, 0, 0, 0);
                                        control.GemData();
                                        (this.Controls.Find("lblS" + nuværendeDeltager.Nr + "R" + rundeNummer + "S", true)[0].Text) = Convert.ToString(næsteDeltager.Nr);
                                        (this.Controls.Find("lblS" + nuværendeDeltager.Nr + "R" + rundeNummer + "H", true)[0].Text) = "";
                                        (this.Controls.Find("lblS" + næsteDeltager.Nr + "R" + rundeNummer + "H", true)[0].Text) = Convert.ToString(nuværendeDeltager.Nr);
                                        (this.Controls.Find("lblS" + næsteDeltager.Nr + "R" + rundeNummer + "S", true)[0].Text) = "";
                                    }
                                    else
                                    {
                                        int sorte1 = control.OptældFarve(nuværendeDeltager.DeltagerID);
                                        int sorte2 = control.OptældFarve(næsteDeltager.DeltagerID);

                                        if (sorte1 <= sorte2)
                                        {
                                            control.FindDeltager(nuværendeDeltager.DeltagerID);
                                            control.RedigerRunde(rundeNummer, næsteDeltager.Nr, 0, 0, 0, 100);
                                            control.FindDeltager(næsteDeltager.DeltagerID);
                                            control.RedigerRunde(rundeNummer, nuværendeDeltager.Nr, 1, 0, 0, 100);
                                            control.GemData();
                                            (this.Controls.Find("lblS" + nuværendeDeltager.Nr + "R" + rundeNummer + "S", true)[0].Text) = Convert.ToString(næsteDeltager.Nr);
                                            (this.Controls.Find("lblS" + nuværendeDeltager.Nr + "R" + rundeNummer + "H", true)[0].Text) = "";
                                            (this.Controls.Find("lblS" + næsteDeltager.Nr + "R" + rundeNummer + "H", true)[0].Text) = Convert.ToString(nuværendeDeltager.Nr);
                                            (this.Controls.Find("lblS" + næsteDeltager.Nr + "R" + rundeNummer + "S", true)[0].Text) = "";
                                        }
                                        else
                                        {
                                            control.FindDeltager(nuværendeDeltager.DeltagerID);
                                            control.RedigerRunde(rundeNummer, næsteDeltager.Nr, 1, 0, 0, 100);
                                            control.FindDeltager(næsteDeltager.DeltagerID);
                                            control.RedigerRunde(rundeNummer, nuværendeDeltager.Nr, 0, 0, 0, 100);
                                            control.GemData();
                                            (this.Controls.Find("lblS" + nuværendeDeltager.Nr + "R" + rundeNummer + "H", true)[0].Text) = Convert.ToString(næsteDeltager.Nr);
                                            (this.Controls.Find("lblS" + nuværendeDeltager.Nr + "R" + rundeNummer + "S", true)[0].Text) = "";
                                            (this.Controls.Find("lblS" + næsteDeltager.Nr + "R" + rundeNummer + "S", true)[0].Text) = Convert.ToString(nuværendeDeltager.Nr);
                                            (this.Controls.Find("lblS" + næsteDeltager.Nr + "R" + rundeNummer + "H", true)[0].Text) = "";
                                        }
                                        
                                    }
                                    
                                    hop = true; // Sætter runden til at være sat
                                }
                            }
                        }
                    }
                }
                tæller++; // Tager næste deltager i listen
            }
        }

        private void BeregnKorrektion(IDeltager aktuelDeltager)
        {
            control.FindDeltager(aktuelDeltager.DeltagerID);
        }

        #region Tekstboxe til label
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (txtS1R1.Text == "1" || txtS1R1.Text == "0" || txtS1R1.Text == "½")
            {
                lblS1R1.Text = txtS1R1.Text;
                txtS1R1.Visible = false;
            }
            else
            {
                txtS1R1.Text = "";
            }

        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            if (txtS1R2.Text == "1" || txtS1R2.Text == "0" || txtS1R2.Text == "½")
            {
                lblS1R2.Text = txtS1R2.Text;
                txtS1R2.Visible = false;
            }
            else
            {
                txtS1R2.Text = "";
            }

        }

        private void textBox21_TextChanged(object sender, EventArgs e)
        {
            if (txtS1R3.Text == "1" || txtS1R3.Text == "0" || txtS1R3.Text == "½")
            {
                lblS1R3.Text = txtS1R3.Text;
                txtS1R3.Visible = false;
            }
            else
            {
                txtS1R3.Text = "";
            }
        }

        private void txtS1R4_TextChanged(object sender, EventArgs e)
        {
            if (txtS1R4.Text == "1" || txtS1R4.Text == "0" || txtS1R4.Text == "½")
            {
                lblS1R4.Text = txtS1R4.Text;
                txtS1R4.Visible = false;
            }
            else
            {
                txtS1R4.Text = "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SkjulTekstBoxe();
        }

        private void txtS2R1_TextChanged(object sender, EventArgs e)
        {
            if (txtS2R1.Text == "1" || txtS2R1.Text == "0" || txtS2R1.Text == "½")
            {
                lblS2R1.Text = txtS2R1.Text;
                txtS2R1.Visible = false;
            }
            else
            {
                txtS2R1.Text = "";
            }
        }

        private void txtS2R2_TextChanged(object sender, EventArgs e)
        {
            if (txtS2R2.Text == "1" || txtS2R2.Text == "0" || txtS2R2.Text == "½")
            {
                lblS2R2.Text = txtS2R2.Text;
                txtS2R2.Visible = false;
            }
            else
            {
                txtS2R2.Text = "";
            }
        }

        private void txtS2R3_TextChanged(object sender, EventArgs e)
        {
            if (txtS2R3.Text == "1" || txtS2R3.Text == "0" || txtS2R3.Text == "½")
            {
                lblS2R3.Text = txtS2R3.Text;
                txtS2R3.Visible = false;
            }
            else
            {
                txtS2R3.Text = "";
            }
        }

        private void txtS3R1_TextChanged(object sender, EventArgs e)
        {
            if (txtS3R1.Text == "1" || txtS3R1.Text == "0" || txtS3R1.Text == "½")
            {
                lblS3R1.Text = txtS3R1.Text;
                txtS3R1.Visible = false;
            }
            else
            {
                txtS3R1.Text = "";
            }
        }

        private void txtS3R2_TextChanged(object sender, EventArgs e)
        {
            if (txtS3R2.Text == "1" || txtS3R2.Text == "0" || txtS3R2.Text == "½")
            {
                lblS3R2.Text = txtS3R2.Text;
                txtS3R2.Visible = false;
            }
            else
            {
                txtS3R2.Text = "";
            }
        }

        private void txtS3R3_TextChanged(object sender, EventArgs e)
        {
            if (txtS3R3.Text == "1" || txtS3R3.Text == "0" || txtS3R3.Text == "½")
            {
                lblS3R3.Text = txtS3R3.Text;
                txtS3R3.Visible = false;
            }
            else
            {
                txtS3R3.Text = "";
            }
        }

        private void txtS2R4_TextChanged(object sender, EventArgs e)
        {
            if (txtS2R4.Text == "1" || txtS2R4.Text == "0" || txtS2R4.Text == "½")
            {
                lblS2R4.Text = txtS2R4.Text;
                txtS2R4.Visible = false;
            }
            else
            {
                txtS2R4.Text = "";
            }
        }

        private void txtS3R4_TextChanged(object sender, EventArgs e)
        {
            if (txtS3R4.Text == "1" || txtS3R4.Text == "0" || txtS3R4.Text == "½")
            {
                lblS3R4.Text = txtS3R4.Text;
                txtS3R4.Visible = false;
            }
            else
            {
                txtS3R4.Text = "";
            }
        }


        private void txtS4R1_TextChanged(object sender, EventArgs e)
        {
            if (txtS4R1.Text == "1" || txtS4R1.Text == "0" || txtS4R1.Text == "½")
            {
                lblS4R1.Text = txtS4R1.Text;
                txtS4R1.Visible = false;
            }
            else
            {
                txtS4R1.Text = "";
            }
        }

        private void txtS4R2_TextChanged(object sender, EventArgs e)
        {
            if (txtS4R2.Text == "1" || txtS4R2.Text == "0" || txtS4R2.Text == "½")
            {
                lblS4R2.Text = txtS4R2.Text;
                txtS4R2.Visible = false;
            }
            else
            {
                txtS4R2.Text = "";
            }
        }

        private void txtS4R3_TextChanged(object sender, EventArgs e)
        {
            if (txtS4R3.Text == "1" || txtS4R3.Text == "0" || txtS4R3.Text == "½")
            {
                lblS4R3.Text = txtS4R3.Text;
                txtS4R3.Visible = false;
            }
            else
            {
                txtS4R3.Text = "";
            }
        }

        private void txtS4R4_TextChanged(object sender, EventArgs e)
        {
            if (txtS4R4.Text == "1" || txtS4R4.Text == "0" || txtS4R4.Text == "½")
            {
                lblS4R4.Text = txtS4R4.Text;
                txtS4R4.Visible = false;
            }
            else
            {
                txtS4R4.Text = "";
            }
        }              

        private void txtS5R1_TextChanged(object sender, EventArgs e)
        {
            if (txtS5R1.Text == "1" || txtS5R1.Text == "0" || txtS5R1.Text == "½")
            {
                lblS5R1.Text = txtS5R1.Text;
                txtS5R1.Visible = false;
            }
            else
            {
                txtS5R1.Text = "";
            }
        }

        private void txtS5R2_TextChanged(object sender, EventArgs e)
        {
            if (txtS5R2.Text == "1" || txtS5R2.Text == "0" || txtS5R2.Text == "½")
            {
                lblS5R2.Text = txtS5R2.Text;
                txtS5R2.Visible = false;
            }
            else
            {
                txtS5R2.Text = "";
            }
        }

        private void txtS5R3_TextChanged(object sender, EventArgs e)
        {
            if (txtS5R3.Text == "1" || txtS5R3.Text == "0" || txtS5R3.Text == "½")
            {
                lblS5R3.Text = txtS5R3.Text;
                txtS5R3.Visible = false;
            }
            else
            {
                txtS5R3.Text = "";
            }
        }

        private void txtS5R4_TextChanged(object sender, EventArgs e)
        {
            if (txtS5R4.Text == "1" || txtS5R4.Text == "0" || txtS5R4.Text == "½")
            {
                lblS5R4.Text = txtS5R4.Text;
                txtS5R4.Visible = false;
            }
            else
            {
                txtS5R4.Text = "";
            }
        }

        private void txtS6R1_TextChanged(object sender, EventArgs e)
        {
            if (txtS6R1.Text == "1" || txtS6R1.Text == "0" || txtS6R1.Text == "½")
            {
                lblS6R1.Text = txtS6R1.Text;
                txtS6R1.Visible = false;
            }
            else
            {
                txtS6R1.Text = "";
            }
        }

        private void txtS6R2_TextChanged(object sender, EventArgs e)
        {
            if (txtS6R2.Text == "1" || txtS6R2.Text == "0" || txtS6R2.Text == "½")
            {
                lblS6R2.Text = txtS6R2.Text;
                txtS6R2.Visible = false;
            }
            else
            {
                txtS6R2.Text = "";
            }
        }

        private void txtS6R3_TextChanged(object sender, EventArgs e)
        {
            if (txtS6R3.Text == "1" || txtS6R3.Text == "0" || txtS6R3.Text == "½")
            {
                lblS6R3.Text = txtS6R3.Text;
                txtS6R3.Visible = false;
            }
            else
            {
                txtS6R3.Text = "";
            }
        }

        private void txtS6R4_TextChanged(object sender, EventArgs e)
        {
            if (txtS6R4.Text == "1" || txtS6R4.Text == "0" || txtS6R4.Text == "½")
            {
                lblS6R4.Text = txtS6R4.Text;
                txtS6R4.Visible = false;
            }
            else
            {
                txtS6R4.Text = "";
            }
        }

        private void txtS7R1_TextChanged(object sender, EventArgs e)
        {
            if (txtS7R1.Text == "1" || txtS7R1.Text == "0" || txtS7R1.Text == "½")
            {
                lblS7R1.Text = txtS7R1.Text;
                txtS7R1.Visible = false;
            }
            else
            {
                txtS7R1.Text = "";
            }
        }

        private void txtS7R2_TextChanged(object sender, EventArgs e)
        {
            if (txtS7R2.Text == "1" || txtS7R2.Text == "0" || txtS7R2.Text == "½")
            {
                lblS7R2.Text = txtS7R2.Text;
                txtS7R2.Visible = false;
            }
            else
            {
                txtS7R2.Text = "";
            }
        }

        private void txtS7R3_TextChanged(object sender, EventArgs e)
        {
            if (txtS7R3.Text == "1" || txtS7R3.Text == "0" || txtS7R3.Text == "½")
            {
                lblS7R3.Text = txtS7R3.Text;
                txtS7R3.Visible = false;
            }
            else
            {
                txtS7R3.Text = "";
            }
        }

        private void txtS7R4_TextChanged(object sender, EventArgs e)
        {
            if (txtS7R4.Text == "1" || txtS7R4.Text == "0" || txtS7R4.Text == "½")
            {
                lblS7R4.Text = txtS7R4.Text;
                txtS7R4.Visible = false;
            }
            else
            {
                txtS7R4.Text = "";
            }
        }

        private void txtS8R1_TextChanged(object sender, EventArgs e)
        {
            if (txtS8R1.Text == "1" || txtS8R1.Text == "0" || txtS8R1.Text == "½")
            {
                lblS8R1.Text = txtS8R1.Text;
                txtS8R1.Visible = false;
            }
            else
            {
                txtS8R1.Text = "";
            }
        }

        private void txtS8R2_TextChanged(object sender, EventArgs e)
        {
            if (txtS8R2.Text == "1" || txtS8R2.Text == "0" || txtS8R2.Text == "½")
            {
                lblS8R2.Text = txtS8R2.Text;
                txtS8R2.Visible = false;
            }
            else
            {
                txtS8R2.Text = "";
            }
        }

        private void txtS8R3_TextChanged(object sender, EventArgs e)
        {
            if (txtS8R3.Text == "1" || txtS8R3.Text == "0" || txtS8R3.Text == "½")
            {
                lblS8R3.Text = txtS8R3.Text;
                txtS8R3.Visible = false;
            }
            else
            {
                txtS8R3.Text = "";
            }
        }

        private void txtS8R4_TextChanged(object sender, EventArgs e)
        {
            if (txtS8R4.Text == "1" || txtS8R4.Text == "0" || txtS8R4.Text == "½")
            {
                lblS8R4.Text = txtS8R4.Text;
                txtS8R4.Visible = false;
            }
            else
            {
                txtS8R4.Text = "";
            }
        }

        private void txtS9R1_TextChanged(object sender, EventArgs e)
        {
            if (txtS9R1.Text == "1" || txtS9R1.Text == "0" || txtS9R1.Text == "½")
            {
                lblS9R1.Text = txtS9R1.Text;
                txtS9R1.Visible = false;
            }
            else
            {
                txtS9R1.Text = "";
            }
        }

        private void txtS9R2_TextChanged(object sender, EventArgs e)
        {
            if (txtS9R2.Text == "1" || txtS9R2.Text == "0" || txtS9R2.Text == "½")
            {
                lblS9R2.Text = txtS9R2.Text;
                txtS9R2.Visible = false;
            }
            else
            {
                txtS9R2.Text = "";
            }
        }

        private void txtS9R3_TextChanged(object sender, EventArgs e)
        {
            if (txtS9R3.Text == "1" || txtS9R3.Text == "0" || txtS9R3.Text == "½")
            {
                lblS9R3.Text = txtS9R3.Text;
                txtS9R3.Visible = false;
            }
            else
            {
                txtS9R3.Text = "";
            }
        }

        private void txtS9R4_TextChanged(object sender, EventArgs e)
        {
            if (txtS9R4.Text == "1" || txtS9R4.Text == "0" || txtS9R4.Text == "½")
            {
                lblS9R4.Text = txtS9R4.Text;
                txtS9R4.Visible = false;
            }
            else
            {
                txtS9R4.Text = "";
            }
        }

        private void txtS10R1_TextChanged(object sender, EventArgs e)
        {
            if (txtS10R1.Text == "1" || txtS10R1.Text == "0" || txtS10R1.Text == "½")
            {
                lblS10R1.Text = txtS10R1.Text;
                txtS10R1.Visible = false;
            }
            else
            {
                txtS10R1.Text = "";
            }
        }

        private void txtS10R2_TextChanged(object sender, EventArgs e)
        {
            if (txtS10R2.Text == "1" || txtS10R2.Text == "0" || txtS10R2.Text == "½")
            {
                lblS10R2.Text = txtS10R2.Text;
                txtS10R2.Visible = false;
            }
            else
            {
                txtS10R2.Text = "";
            }
        }

        private void txtS10R3_TextChanged(object sender, EventArgs e)
        {
            if (txtS10R3.Text == "1" || txtS10R3.Text == "0" || txtS10R3.Text == "½")
            {
                lblS10R3.Text = txtS10R3.Text;
                txtS10R3.Visible = false;
            }
            else
            {
                txtS10R3.Text = "";
            }
        }

        private void txtS10R4_TextChanged(object sender, EventArgs e)
        {
            if (txtS10R4.Text == "1" || txtS10R4.Text == "0" || txtS10R4.Text == "½")
            {
                lblS10R4.Text = txtS10R4.Text;
                txtS10R4.Visible = false;
            }
            else
            {
                txtS10R4.Text = "";
            }
        }

        private void txtS1R5_TextChanged(object sender, EventArgs e)
        {
            if (txtS1R5.Text == "1" || txtS1R5.Text == "0" || txtS1R5.Text == "½")
            {
                lblS1R5.Text = txtS1R5.Text;
                txtS1R5.Visible = false;
            }
            else
            {
                txtS1R5.Text = "";
            }
        }

        private void txtS1R6_TextChanged(object sender, EventArgs e)
        {
            if (txtS1R6.Text == "1" || txtS1R6.Text == "0" || txtS1R6.Text == "½")
            {
                lblS1R6.Text = txtS1R6.Text;
                txtS1R6.Visible = false;
            }
            else
            {
                txtS1R6.Text = "";
            }
        }

        private void txtS1R7_TextChanged(object sender, EventArgs e)
        {
            if (txtS1R7.Text == "1" || txtS1R7.Text == "0" || txtS1R7.Text == "½")
            {
                lblS1R7.Text = txtS1R7.Text;
                txtS1R7.Visible = false;
            }
            else
            {
                txtS1R7.Text = "";
            }
        }

        private void txtS1R8_TextChanged(object sender, EventArgs e)
        {
            if (txtS1R8.Text == "1" || txtS1R8.Text == "0" || txtS1R8.Text == "½")
            {
                lblS1R8.Text = txtS1R8.Text;
                txtS1R8.Visible = false;
            }
            else
            {
                txtS1R8.Text = "";
            }
        }

        private void txtS1R9_TextChanged(object sender, EventArgs e)
        {
            if (txtS1R9.Text == "1" || txtS1R9.Text == "0" || txtS1R9.Text == "½")
            {
                lblS1R9.Text = txtS1R9.Text;
                txtS1R9.Visible = false;
            }
            else
            {
                txtS1R9.Text = "";
            }
        }

        private void txtS2R5_TextChanged(object sender, EventArgs e)
        {
            if (txtS2R5.Text == "1" || txtS2R5.Text == "0" || txtS2R5.Text == "½")
            {
                lblS2R5.Text = txtS2R5.Text;
                txtS2R5.Visible = false;
            }
            else
            {
                txtS2R5.Text = "";
            }
        }

        private void txtS2R6_TextChanged(object sender, EventArgs e)
        {
            if (txtS2R6.Text == "1" || txtS2R6.Text == "0" || txtS2R6.Text == "½")
            {
                lblS2R6.Text = txtS2R6.Text;
                txtS2R6.Visible = false;
            }
            else
            {
                txtS2R6.Text = "";
            }
        }

        private void txtS2R7_TextChanged(object sender, EventArgs e)
        {
            if (txtS2R7.Text == "1" || txtS2R7.Text == "0" || txtS2R7.Text == "½")
            {
                lblS2R7.Text = txtS2R7.Text;
                txtS2R7.Visible = false;
            }
            else
            {
                txtS2R7.Text = "";
            }
        }

        private void txtS2R8_TextChanged(object sender, EventArgs e)
        {
            if (txtS2R8.Text == "1" || txtS2R8.Text == "0" || txtS2R8.Text == "½")
            {
                lblS2R8.Text = txtS2R8.Text;
                txtS2R8.Visible = false;
            }
            else
            {
                txtS2R8.Text = "";
            }
        }

        private void txtS2R9_TextChanged(object sender, EventArgs e)
        {
            if (txtS2R9.Text == "1" || txtS2R9.Text == "0" || txtS2R9.Text == "½")
            {
                lblS2R9.Text = txtS2R9.Text;
                txtS2R9.Visible = false;
            }
            else
            {
                txtS2R9.Text = "";
            }
        }

        private void txtS3R5_TextChanged(object sender, EventArgs e)
        {
            if (txtS3R5.Text == "1" || txtS3R5.Text == "0" || txtS3R5.Text == "½")
            {
                lblS3R5.Text = txtS3R5.Text;
                txtS3R5.Visible = false;
            }
            else
            {
                txtS3R5.Text = "";
            }
        }

        private void txtS3R6_TextChanged(object sender, EventArgs e)
        {
            if (txtS3R6.Text == "1" || txtS3R6.Text == "0" || txtS3R6.Text == "½")
            {
                lblS3R6.Text = txtS3R6.Text;
                txtS3R6.Visible = false;
            }
            else
            {
                txtS3R6.Text = "";
            }
        }

        private void txtS3R7_TextChanged(object sender, EventArgs e)
        {
            if (txtS3R7.Text == "1" || txtS3R7.Text == "0" || txtS3R7.Text == "½")
            {
                lblS3R7.Text = txtS3R7.Text;
                txtS3R7.Visible = false;
            }
            else
            {
                txtS3R7.Text = "";
            }
        }

        private void txtS3R8_TextChanged(object sender, EventArgs e)
        {
            if (txtS3R8.Text == "1" || txtS3R8.Text == "0" || txtS3R8.Text == "½")
            {
                lblS3R8.Text = txtS3R8.Text;
                txtS3R8.Visible = false;
            }
            else
            {
                txtS3R8.Text = "";
            }
        }

        private void txtS3R9_TextChanged(object sender, EventArgs e)
        {
            if (txtS3R9.Text == "1" || txtS3R9.Text == "0" || txtS3R9.Text == "½")
            {
                lblS3R9.Text = txtS3R9.Text;
                txtS3R9.Visible = false;
            }
            else
            {
                txtS3R9.Text = "";
            }
        }

        private void txtS4R5_TextChanged(object sender, EventArgs e)
        {
            if (txtS4R5.Text == "1" || txtS4R5.Text == "0" || txtS4R5.Text == "½")
            {
                lblS4R5.Text = txtS4R5.Text;
                txtS4R5.Visible = false;
            }
            else
            {
                txtS4R5.Text = "";
            }
        }

        private void txtS4R6_TextChanged(object sender, EventArgs e)
        {
            if (txtS4R6.Text == "1" || txtS4R6.Text == "0" || txtS4R6.Text == "½")
            {
                lblS4R6.Text = txtS4R6.Text;
                txtS4R6.Visible = false;
            }
            else
            {
                txtS4R6.Text = "";
            }
        }

        private void txtS4R7_TextChanged(object sender, EventArgs e)
        {
            if (txtS4R7.Text == "1" || txtS4R7.Text == "0" || txtS4R7.Text == "½")
            {
                lblS4R7.Text = txtS4R7.Text;
                txtS4R7.Visible = false;
            }
            else
            {
                txtS4R7.Text = "";
            }
        }

        private void txtS4R8_TextChanged(object sender, EventArgs e)
        {
            if (txtS4R8.Text == "1" || txtS4R8.Text == "0" || txtS4R8.Text == "½")
            {
                lblS4R8.Text = txtS4R8.Text;
                txtS4R8.Visible = false;
            }
            else
            {
                txtS4R8.Text = "";
            }
        }

        private void txtS4R9_TextChanged(object sender, EventArgs e)
        {
            if (txtS4R9.Text == "1" || txtS4R9.Text == "0" || txtS4R9.Text == "½")
            {
                lblS4R9.Text = txtS4R9.Text;
                txtS4R9.Visible = false;
            }
            else
            {
                txtS4R9.Text = "";
            }
        }

        private void txtS5R5_TextChanged(object sender, EventArgs e)
        {
            if (txtS5R5.Text == "1" || txtS5R5.Text == "0" || txtS5R5.Text == "½")
            {
                lblS5R5.Text = txtS5R5.Text;
                txtS5R5.Visible = false;
            }
            else
            {
                txtS5R5.Text = "";
            }
        }

        private void txtS5R6_TextChanged(object sender, EventArgs e)
        {
            if (txtS5R6.Text == "1" || txtS5R6.Text == "0" || txtS5R6.Text == "½")
            {
                lblS5R6.Text = txtS5R6.Text;
                txtS5R6.Visible = false;
            }
            else
            {
                txtS5R6.Text = "";
            }
        }

        private void txtS5R7_TextChanged(object sender, EventArgs e)
        {
            if (txtS5R7.Text == "1" || txtS5R7.Text == "0" || txtS5R7.Text == "½")
            {
                lblS5R7.Text = txtS5R7.Text;
                txtS5R7.Visible = false;
            }
            else
            {
                txtS5R7.Text = "";
            }
        }

        private void txtS5R8_TextChanged(object sender, EventArgs e)
        {
            if (txtS5R8.Text == "1" || txtS5R8.Text == "0" || txtS5R8.Text == "½")
            {
                lblS5R8.Text = txtS5R8.Text;
                txtS5R8.Visible = false;
            }
            else
            {
                txtS5R8.Text = "";
            }
        }

        private void txtS5R9_TextChanged(object sender, EventArgs e)
        {
            if (txtS5R9.Text == "1" || txtS5R9.Text == "0" || txtS5R9.Text == "½")
            {
                lblS5R9.Text = txtS5R9.Text;
                txtS5R9.Visible = false;
            }
            else
            {
                txtS5R9.Text = "";
            }
        }

        private void txtS6R5_TextChanged(object sender, EventArgs e)
        {
            if (txtS6R5.Text == "1" || txtS6R5.Text == "0" || txtS6R5.Text == "½")
            {
                lblS6R5.Text = txtS6R5.Text;
                txtS6R5.Visible = false;
            }
            else
            {
                txtS6R5.Text = "";
            }
        }

        private void txtS6R6_TextChanged(object sender, EventArgs e)
        {
            if (txtS6R6.Text == "1" || txtS6R6.Text == "0" || txtS6R6.Text == "½")
            {
                lblS6R6.Text = txtS6R6.Text;
                txtS6R6.Visible = false;
            }
            else
            {
                txtS6R6.Text = "";
            }
        }

        private void txtS6R7_TextChanged(object sender, EventArgs e)
        {
            if (txtS6R7.Text == "1" || txtS6R7.Text == "0" || txtS6R7.Text == "½")
            {
                lblS6R7.Text = txtS6R7.Text;
                txtS6R7.Visible = false;
            }
            else
            {
                txtS6R7.Text = "";
            }
        }

        private void txtS6R8_TextChanged(object sender, EventArgs e)
        {
            if (txtS6R8.Text == "1" || txtS6R8.Text == "0" || txtS6R8.Text == "½")
            {
                lblS6R8.Text = txtS6R8.Text;
                txtS6R8.Visible = false;
            }
            else
            {
                txtS6R8.Text = "";
            }
        }

        private void txtS6R9_TextChanged(object sender, EventArgs e)
        {
            if (txtS6R9.Text == "1" || txtS6R9.Text == "0" || txtS6R9.Text == "½")
            {
                lblS6R9.Text = txtS6R9.Text;
                txtS6R9.Visible = false;
            }
            else
            {
                txtS6R9.Text = "";
            }
        }

        private void txtS7R5_TextChanged(object sender, EventArgs e)
        {
            if (txtS7R5.Text == "1" || txtS7R5.Text == "0" || txtS7R5.Text == "½")
            {
                lblS7R5.Text = txtS7R5.Text;
                txtS7R5.Visible = false;
            }
            else
            {
                txtS7R5.Text = "";
            }
        }

        private void txtS7R6_TextChanged(object sender, EventArgs e)
        {
            if (txtS7R6.Text == "1" || txtS7R6.Text == "0" || txtS7R6.Text == "½")
            {
                lblS7R6.Text = txtS7R6.Text;
                txtS7R6.Visible = false;
            }
            else
            {
                txtS7R6.Text = "";
            }
        }

        private void txtS7R7_TextChanged(object sender, EventArgs e)
        {
            if (txtS7R7.Text == "1" || txtS7R7.Text == "0" || txtS7R7.Text == "½")
            {
                lblS7R7.Text = txtS7R7.Text;
                txtS7R7.Visible = false;
            }
            else
            {
                txtS7R7.Text = "";
            }
        }

        private void txtS7R8_TextChanged(object sender, EventArgs e)
        {
            if (txtS7R8.Text == "1" || txtS7R8.Text == "0" || txtS7R8.Text == "½")
            {
                lblS7R8.Text = txtS7R8.Text;
                txtS7R8.Visible = false;
            }
            else
            {
                txtS7R8.Text = "";
            }
        }

        private void txtS7R9_TextChanged(object sender, EventArgs e)
        {
            if (txtS7R9.Text == "1" || txtS7R9.Text == "0" || txtS7R9.Text == "½")
            {
                lblS7R9.Text = txtS7R9.Text;
                txtS7R9.Visible = false;
            }
            else
            {
                txtS7R9.Text = "";
            }
        }

        private void txtS8R5_TextChanged(object sender, EventArgs e)
        {
            if (txtS8R5.Text == "1" || txtS8R5.Text == "0" || txtS8R5.Text == "½")
            {
                lblS8R5.Text = txtS8R5.Text;
                txtS8R5.Visible = false;
            }
            else
            {
                txtS8R5.Text = "";
            }
        }

        private void txtS8R6_TextChanged(object sender, EventArgs e)
        {
            if (txtS8R6.Text == "1" || txtS8R6.Text == "0" || txtS8R6.Text == "½")
            {
                lblS8R6.Text = txtS8R6.Text;
                txtS8R6.Visible = false;
            }
            else
            {
                txtS8R6.Text = "";
            }
        }

        private void txtS8R7_TextChanged(object sender, EventArgs e)
        {
            if (txtS8R7.Text == "1" || txtS8R7.Text == "0" || txtS8R7.Text == "½")
            {
                lblS8R7.Text = txtS8R7.Text;
                txtS8R7.Visible = false;
            }
            else
            {
                txtS8R7.Text = "";
            }
        }

        private void txtS8R8_TextChanged(object sender, EventArgs e)
        {
            if (txtS8R8.Text == "1" || txtS8R8.Text == "0" || txtS8R8.Text == "½")
            {
                lblS8R8.Text = txtS8R8.Text;
                txtS8R8.Visible = false;
            }
            else
            {
                txtS8R8.Text = "";
            }
        }

        private void txtS8R9_TextChanged(object sender, EventArgs e)
        {
            if (txtS8R9.Text == "1" || txtS8R9.Text == "0" || txtS8R9.Text == "½")
            {
                lblS8R9.Text = txtS8R9.Text;
                txtS8R9.Visible = false;
            }
            else
            {
                txtS8R9.Text = "";
            }
        }

        private void txtS10R5_TextChanged(object sender, EventArgs e)
        {
            if (txtS10R5.Text == "1" || txtS10R5.Text == "0" || txtS10R5.Text == "½")
            {
                lblS10R5.Text = txtS10R5.Text;
                txtS10R5.Visible = false;
            }
            else
            {
                txtS10R5.Text = "";
            }
        }

        private void txtS10R6_TextChanged(object sender, EventArgs e)
        {
            if (txtS10R6.Text == "1" || txtS10R6.Text == "0" || txtS10R6.Text == "½")
            {
                lblS10R6.Text = txtS10R6.Text;
                txtS10R6.Visible = false;
            }
            else
            {
                txtS10R6.Text = "";
            }
        }

        private void txtS10R7_TextChanged(object sender, EventArgs e)
        {
            if (txtS10R7.Text == "1" || txtS10R7.Text == "0" || txtS10R7.Text == "½")
            {
                lblS10R7.Text = txtS10R7.Text;
                txtS10R7.Visible = false;
            }
            else
            {
                txtS10R7.Text = "";
            }
        }

        private void txtS10R8_TextChanged(object sender, EventArgs e)
        {
            if (txtS10R8.Text == "1" || txtS10R8.Text == "0" || txtS10R8.Text == "½")
            {
                lblS10R8.Text = txtS10R8.Text;
                txtS10R8.Visible = false;
            }
            else
            {
                txtS10R8.Text = "";
            }
        }

        private void txtS10R9_TextChanged(object sender, EventArgs e)
        {
            if (txtS10R9.Text == "1" || txtS10R9.Text == "0" || txtS10R9.Text == "½")
            {
                lblS10R9.Text = txtS10R9.Text;
                txtS10R9.Visible = false;
            }
            else
            {
                txtS10R9.Text = "";
            }
        }
        

        private void txtS9R5_TextChanged(object sender, EventArgs e)
        {
            if (txtS9R5.Text == "1" || txtS9R5.Text == "0" || txtS9R5.Text == "½")
            {
                lblS9R5.Text = txtS9R5.Text;
                txtS9R5.Visible = false;
            }
            else
            {
                txtS9R5.Text = "";
            }
        }

        private void txtS9R6_TextChanged(object sender, EventArgs e)
        {
            if (txtS9R6.Text == "1" || txtS9R6.Text == "0" || txtS9R6.Text == "½")
            {
                lblS9R6.Text = txtS9R6.Text;
                txtS9R6.Visible = false;
            }
            else
            {
                txtS9R6.Text = "";
            }
        }

        private void txtS9R7_TextChanged(object sender, EventArgs e)
        {
            if (txtS9R7.Text == "1" || txtS9R7.Text == "0" || txtS9R7.Text == "½")
            {
                lblS9R7.Text = txtS9R7.Text;
                txtS9R7.Visible = false;
            }
            else
            {
                txtS9R7.Text = "";
            }
        }

        private void txtS9R8_TextChanged(object sender, EventArgs e)
        {
            if (txtS9R8.Text == "1" || txtS9R8.Text == "0" || txtS9R8.Text == "½")
            {
                lblS9R8.Text = txtS9R8.Text;
                txtS9R8.Visible = false;
            }
            else
            {
                txtS9R8.Text = "";
            }
        }

        private void txtS9R9_TextChanged(object sender, EventArgs e)
        {
            if (txtS9R9.Text == "1" || txtS9R9.Text == "0" || txtS9R9.Text == "½")
            {
                lblS9R9.Text = txtS9R9.Text;
                txtS9R9.Visible = false;
            }
            else
            {
                txtS9R9.Text = "";
            }
        }

        #endregion


    }
        
}



 